<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>

</body>
<script>
    // function ajax() {
    //     var xhr = new XMLHttpRequest()
    //     xhr.open('请求方式', '请求地址', 'true异步/false同步')
    //     xhr.send()
    //     xhr.onreadystatechange = function () {
    //         if (xhr.readyState == 4 && xhr.status == 200) {
    //             console.log(xhr.responseText);
    //         }
    //     }
    // }
    function ajax(type, url, success) {
        let p = new Promise(function (resolve, reject) {
            var xhr = new XMLHttpRequest()
            xhr.open(type, url, true)
            xhr.send()
            xhr.onreadystatechange = function () {
                if (xhr.readyState == 4 && xhr.status == 200) {
                    resolve(xhr.responseText)
                    // reject()
                }
            }
        })
        return p
    }
    ajax('GET', './1.json').then(function(date){
        console.log(date);
        return  ajax('GET', './2.json')
    })
    .then(function(date){
        console.log(date);
    })
    .catch(function(){
        console.log('捕获错误');
    })
    .finally(function(){
        console.log('成功失败都会打印');
    })


    let a=ajax('GET', './1.json')
    let b=ajax('GET', './2.json')

    // 合并 都对输出，有错全错
    Promise.all([a,b]).then(function(d){
        console.log('啊 是',d);
    })

    // 竞争：哪个先改变输出哪个
    Promise.race([a,b]).then(function(d){
        console.log('将',d);
    })


    let p2=Promise.reject(['aaa','ccc'])
    p2.then((data)=>{
        console.log('成功',data);
    },(data)=>{
        console.log('失败',data);
    })
    p2.catch((data)=>{
        console.log('失败',data);
    })

    let p3=Promise.resolve(['vvv','xxxx'])
    p3.then((date)=>{
        console.log('成功',date);
    })
    
</script>

</html>